package com.young.shop.api.controller;

import com.young.shop.api.exceptions.ParamsException;
import com.young.shop.api.service.UserService;
import com.young.shop.api.utils.EmailUtil;
import com.young.shop.pojo.entity.Address;
import com.young.shop.pojo.entity.User;
import com.young.shop.pojo.vo.BaseRespVO;
import com.young.shop.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author 曾家乐
 * @version 1.0
 * @date 2021-01-08 10:23
 */
@Api(tags = "用户业务")
@RestController
//@RequestMapping("/user")
public class UserController extends BaseController {
    @Autowired
    private UserService userService;

    private Logger logger = LoggerFactory.getLogger(UserController.class);
    private Map<String, String> codeMap = new HashMap<String, String>();

    @ApiOperation("用户登录")
    @PostMapping("login")
    @ResponseBody
    public BaseRespVO login(String userName , String password) {
        System.out.println("进入login");

        BaseRespVO<Object> baseRespVO = new BaseRespVO<>();

        //通过try catch捕获service层的异常，如果service层抛出异常，登录失败
        try{
            //调用service层的登录方法
            UserVO userVO = userService.userLogin(userName , password);

            //设置baseRespVO返回的对象
            baseRespVO.setData(userVO);
        }catch (ParamsException p){
            baseRespVO.setCode(BaseRespVO.CodeType.FAILED.getValue());
            baseRespVO.setMsg(p.getMsg());
            p.printStackTrace();
        }/*catch (Exception e){
            baseRespVO.setCode(BaseRespVO.CodeType.FAILED.getValue());
            baseRespVO.setMsg("登陆失败！");
        }*/
        return baseRespVO;
    }

    @ApiOperation("发送邮件")
    @GetMapping("sendEmail")
    @ResponseBody
    public void sendEmail(
            @ApiParam(value = "邮箱账号", required = true) String email) {

        String emailCode = EmailUtil.SendToEmail(email);
        System.out.println(emailCode);
        codeMap.put(email , emailCode);
    }

    /**
     * String userName,String email,String password,String code
     *
     * @param user
     * @return
     */
    @ApiOperation("用户注册")
    @PostMapping("register")
    @ResponseBody
    public BaseRespVO register(User user ,String code , boolean check) {
        BaseRespVO baseRespVO = new BaseRespVO();
        System.out.println("进入注册页面");
        try {
            UserVO userVO = userService.userRegister(user , code , codeMap , check);
            baseRespVO.setData(userVO);
        } catch (ParamsException p) {
            baseRespVO.setCode(BaseRespVO.CodeType.FAILED.getValue());
            baseRespVO.setMsg(p.getMsg());
            p.printStackTrace();
        } catch (Exception e) {
            baseRespVO.setCode(BaseRespVO.CodeType.FAILED.getValue());
            baseRespVO.setMsg("注册失败！");
        }
        return baseRespVO;
    }

    /**
     * 修改密码
     *
     * @param oldPassword    原来的密码
     * @param newPassword    新密码
     * @param repeatPassword 重复密码
     * @param id             前端发过来的用户id
     */
    @ApiOperation("用户密码修改")
    @PostMapping("updatePwd")
    @ResponseBody
    public BaseRespVO updateUserPassword(String oldPassword , String newPassword , String repeatPassword , long id) {
        BaseRespVO baseRespVO = new BaseRespVO();
        System.out.println("oldPassword:" + oldPassword);
        System.out.println("newPassword:" + newPassword);
        System.out.println("repeatPassword:" + repeatPassword);
        System.out.println("id:" + id);
        try {
            // 调用Service层修改密码方法
            userService.updatePassWord(id , oldPassword , newPassword , repeatPassword);

        } catch (ParamsException p) {
            baseRespVO.setCode(BaseRespVO.CodeType.FAILED.getValue());
            baseRespVO.setMsg(p.getMsg());
            p.printStackTrace();
        } catch (Exception e) {
            baseRespVO.setCode(BaseRespVO.CodeType.FAILED.getValue());
            baseRespVO.setMsg("修改失败！");
        }
        return baseRespVO;
    }


    @ApiOperation("添加用户地址")
    @GetMapping("address")
    @ResponseBody
    public BaseRespVO addressList(Address address) {
        System.out.println("进入address");
        BaseRespVO baseRespVO = new BaseRespVO();

        try {
            userService.userAddress(address);
            baseRespVO.setData(address);
        } catch (ParamsException p) {
            baseRespVO.setCode(BaseRespVO.CodeType.FAILED.getValue());
            baseRespVO.setMsg(p.getMsg());
            p.printStackTrace();
        } catch (Exception e) {
            baseRespVO.setCode(BaseRespVO.CodeType.FAILED.getValue());
            baseRespVO.setMsg("添加失败，请稍后再试！");
        }
        return baseRespVO;
    }

    @ApiOperation("展示用户地址")
    @GetMapping("showAddress")
    @ResponseBody
    public BaseRespVO showAddress(long userId) {
        System.out.println("userId:" + userId);
        List addressList = userService.showAddressList(userId);
        BaseRespVO baseRespVO = new BaseRespVO();
        addressList.forEach(item -> {
            System.out.println(item);
        });
        baseRespVO.setData(addressList);
        return baseRespVO;
    }

    @ApiOperation("删除用户地址")
    @PostMapping("delAddress")
    @ResponseBody
    public BaseRespVO delAddress(long userId , long id) {
        BaseRespVO baseRespVO = new BaseRespVO();
        try {
            userService.delAdd(userId , id);
        } catch (ParamsException p) {
            baseRespVO.setCode(BaseRespVO.CodeType.FAILED.getValue());
            baseRespVO.setMsg(p.getMsg());
            p.printStackTrace();
        } catch (Exception e) {
            baseRespVO.setCode(BaseRespVO.CodeType.FAILED.getValue());
            baseRespVO.setMsg("删除失败，请稍后再试！");
        }
        return baseRespVO;
    }

    @ApiOperation("显示用户信息")
    @GetMapping("information")
    @ResponseBody
    public BaseRespVO information(long id) {
        BaseRespVO baseRespVO = new BaseRespVO();
        try {
            UserVO userVO = userService.showInformation(id);
            baseRespVO.setData(userVO);
        } catch (ParamsException p) {
            baseRespVO.setCode(BaseRespVO.CodeType.FAILED.getValue());
            baseRespVO.setMsg(p.getMsg());
            p.printStackTrace();
        } catch (Exception e) {
            baseRespVO.setCode(BaseRespVO.CodeType.FAILED.getValue());
            baseRespVO.setMsg("系统错误，请稍后再试！");
        }

        return baseRespVO;
    }

    @ApiOperation("修改用户信息")
    @PostMapping("updateInformation")
    @ResponseBody
    public BaseRespVO updateInformation(User user) {
        System.out.println("进入updateInformation");
        BaseRespVO baseRespVO = new BaseRespVO();
        try {
            userService.updateInformation(user);
        } catch (ParamsException p) {
            baseRespVO.setCode(BaseRespVO.CodeType.FAILED.getValue());
            baseRespVO.setMsg(p.getMsg());
            p.printStackTrace();
        } catch (Exception e) {
            baseRespVO.setCode(BaseRespVO.CodeType.FAILED.getValue());
            baseRespVO.setMsg("系统错误，请稍后再试！");
        }

        return baseRespVO;
    }

    @ApiOperation("修改用户地址")
    @PostMapping("updateAddress")
    @ResponseBody
    public BaseRespVO updateAddress(Address address) {
        System.out.println("进入updateAddress");
        System.out.println("address:" + address);
        BaseRespVO baseRespVO = new BaseRespVO();
        try {
            userService.updateAddress(address);
        } catch (ParamsException p) {
            baseRespVO.setCode(BaseRespVO.CodeType.FAILED.getValue());
            baseRespVO.setMsg(p.getMsg());
            p.printStackTrace();
        } catch (Exception e) {
            baseRespVO.setCode(BaseRespVO.CodeType.FAILED.getValue());
            baseRespVO.setMsg("修改失败，请稍后再试！");
        }
        return baseRespVO;
    }

    @ApiOperation("根据地址id查询到地址信息")
    @GetMapping("selectAddressById")
    @ResponseBody
    public BaseRespVO selectAddressById(long id) {
        BaseRespVO baseRespVO = new BaseRespVO();
        System.out.println("selectAddressById:" + id);
        Address address = userService.selectAddressById(id);
        baseRespVO.setData(address);
        return baseRespVO;
    }
}
